package org.gary.equipment.service.impl;

import static lengreen.util.StringUtils.isNull;

import java.util.List;

import javax.annotation.Resource;

import org.gary.base.model.Image;
import org.gary.base.service.ImageService;
import org.gary.core.factories.impl.BasicServiceFactoryImpl;
import org.gary.core.util.ResultMessage;
import org.gary.equipment.dao.EquipmentDao;
import org.gary.equipment.model.Equipment;
import org.gary.equipment.service.EquipmentService;
import org.springframework.stereotype.Component;

/**
 * 设备Service实现类
 * @author lengreen
 *
 */
@Component("equipmentService")
public class EquipmentServiceImpl extends BasicServiceFactoryImpl<Equipment>
		implements EquipmentService {
	/**
	 * 用于图片日志记录
	 */
	private ImageService imageService;
	private EquipmentDao equipmentDao;

	/**
	 * 重写basicServicefactoryimpl中的Save方法， 重写以加入日志模块，并记录日志
	 */
	@Override
	public String save(Equipment equipment) {
		if (isNull(equipment.getName())) {
			return ResultMessage.equipment.ADD_NONE;
		} else {
			for (Image image : equipment.getImages()) {
				imageService.save(image);
			}
			// 在设备保存之前记录一条日志
			getLoggerService().makeLog(equipment.getClass().getName(),
					"开始保存一个设备，" + equipment);
			super.save(equipment);
			// 在设备保存之后记录一条日志
			getLoggerService().makeLog(equipment.getClass().getName(),
					"保存一个设备成功，" + equipment);
			return ResultMessage.product.ADD_SUCCESS;
		}
	}
	
	@Override
	public List<Image> loadImages(int equipmentId) {
		return equipmentDao.loadImages(equipmentId);
	}

	@Resource
	public void setImageService(ImageService imageService) {
		this.imageService = imageService;
	}

	public ImageService getImageService() {
		return imageService;
	}

	@Resource
	public void setEquipmentDao(EquipmentDao equipmentDao) {
		this.equipmentDao = equipmentDao;
	}

	public EquipmentDao getEquipmentDao() {
		return equipmentDao;
	}

	@Override
	public List<Equipment> getEquipmentList(int top, int page, Object lang,
			int type) {
		// TODO Auto-generated method stub
		return list(Equipment.class, top, page, lang, type);
	}
}
